package com.sls.game.parser.tencent;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sls.core.parser.Parser;
import com.sls.game.pojo.TencentGoodsInfo;

/**
 * 
 * @description 解析读取腾讯配置文件中的内容
 * @author Wangxuegang
 * @date 2012-10-25
 * 
 */
public class ParseTencentConfigFile {

	private static Logger log = Logger.getLogger(ParseTencentConfigFile.class);

	/**
	 * 读取腾讯配置文件中的内容
	 * 
	 */
	public static TencentGoodsInfo readTencentConfigFile() {
		InputStream xmlStream = Parser.class.getClassLoader()
				.getResourceAsStream(
						"conf" + File.separator + "tencentServer.xml");
		TencentGoodsInfo info = new TencentGoodsInfo();
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(xmlStream);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("address".equals(node.getNodeName())) { // 第二层节点
					String address = node.getTextContent();
					info.setAddress(address.trim());
				} else if ("order_address".equals(node.getNodeName())) {
					String order_address = node.getTextContent();
					info.setOrderAddress(order_address.trim());
				} else if ("mch_id".equals(node.getNodeName())) { 
					String mch_id = node.getTextContent();
					info.setMch_id(mch_id.trim());
				} else if ("enc_key".equals(node.getNodeName())) { 
					String enc_key = node.getTextContent();
					info.setEnc_key(enc_key.trim());
				} else if ("notifyUrl".equals(node.getNodeName())) { 
					String notifyUrl = node.getTextContent();
					info.setNotifyUrl(notifyUrl.trim());
				} else if ("proviceName".equals(node.getNodeName())) { 
					String proviceName = node.getTextContent();
					info.setProviceName(proviceName);
				} else if ("mch_id_zhejiang".equals(node.getNodeName())) { 
					String mch_id_zhejiang = node.getTextContent();
					info.setMch_id_zhejiang(mch_id_zhejiang);
				} else if ("enc_key_zhejiang".equals(node.getNodeName())) { 
					String enc_key_zhejiang = node.getTextContent();
					info.setEnc_key_zhejiang(enc_key_zhejiang);
				} else if ("mch_id_guangdong".equals(node.getNodeName())) { 
					String mch_id_guangdong = node.getTextContent();
					info.setMch_id_guangdong(mch_id_guangdong);
				} else if ("enc_key_guangdong".equals(node.getNodeName())) { 
					String enc_key_guangdong = node.getTextContent();
					info.setEnc_key_guangdong(enc_key_guangdong);
				} else if ("mch_id_guangxi".equals(node.getNodeName())) { 
					String mch_id_guangxi = node.getTextContent();
					info.setMch_id_guangxi(mch_id_guangxi);
				} else if ("enc_key_guangxi".equals(node.getNodeName())) { 
					String enc_key_guangxi = node.getTextContent();
					info.setEnc_key_guangxi(enc_key_guangxi);
				} else if ("mch_id_yunnan".equals(node.getNodeName())) { 
					String mch_id_yunnan = node.getTextContent();
					info.setMch_id_yunnan(mch_id_yunnan);
				} else if ("enc_key_yunnan".equals(node.getNodeName())) { 
					String enc_key_yunnan = node.getTextContent();
					info.setEnc_key_yunnan(enc_key_yunnan);
				} else if ("mch_id_shandong".equals(node.getNodeName())) { 
					String mch_id_shandong = node.getTextContent();
					info.setMch_id_shandong(mch_id_shandong);
				} else if ("enc_key_shandong".equals(node.getNodeName())) { 
					String enc_key_shandong = node.getTextContent();
					info.setEnc_key_shandong(enc_key_shandong);
				} else if ("mch_id_jiangsu".equals(node.getNodeName())) { 
					String mch_id_jiangsu = node.getTextContent();
					info.setMch_id_jiangsu(mch_id_jiangsu);
				} else if ("enc_key_jiangsu".equals(node.getNodeName())) { 
					String enc_key_jiangsu = node.getTextContent();
					info.setEnc_key_jiangsu(enc_key_jiangsu);
				} else if ("mch_id_jiangxi".equals(node.getNodeName())) { 
					String mch_id_jiangxi = node.getTextContent();
					info.setMch_id_jiangxi(mch_id_jiangxi);
				} else if ("enc_key_jiangxi".equals(node.getNodeName())) { 
					String enc_key_jiangxi = node.getTextContent();
					info.setEnc_key_jiangxi(enc_key_jiangxi);
				} else if ("mch_id_hubei".equals(node.getNodeName())) { 
					String mch_id_hubei = node.getTextContent();
					info.setMch_id_hubei(mch_id_hubei);
				} else if ("enc_key_hubei".equals(node.getNodeName())) { 
					String enc_key_hubei = node.getTextContent();
					info.setEnc_key_hubei(enc_key_hubei);
				} else if ("mch_id_sichuan".equals(node.getNodeName())) { 
					String mch_id_sichuan = node.getTextContent();
					info.setMch_id_sichuan(mch_id_sichuan);
				} else if ("enc_key_sichuan".equals(node.getNodeName())) { 
					String enc_key_sichuan = node.getTextContent();
					info.setEnc_key_sichuan(enc_key_sichuan);
				} else if ("mch_id_henan".equals(node.getNodeName())) { 
					String mch_id_henan = node.getTextContent();
					info.setMch_id_henan(mch_id_henan);
				} else if ("enc_key_henan".equals(node.getNodeName())) { 
					String enc_key_henan = node.getTextContent();
					info.setEnc_key_henan(enc_key_henan);
				} else if ("mch_id_shanghai".equals(node.getNodeName())) { 
					String mch_id_shanghai = node.getTextContent();
					info.setMch_id_shanghai(mch_id_shanghai);
				} else if ("enc_key_shanghai".equals(node.getNodeName())) { 
					String enc_key_shanghai = node.getTextContent();
					info.setEnc_key_shanghai(enc_key_shanghai);
				} else if ("mch_id_hunan".equals(node.getNodeName())) { 
					String mch_id_hunan = node.getTextContent();
					info.setMch_id_hunan(mch_id_hunan);
				} else if ("enc_key_hunan".equals(node.getNodeName())) { 
					String enc_key_hunan = node.getTextContent();
					info.setEnc_key_hunan(enc_key_hunan);
				}else if ("mch_id_anhui".equals(node.getNodeName())) { 
					String mch_id_anhui = node.getTextContent();
					info.setMch_id_anhui(mch_id_anhui);
				}else if ("enc_key_anhui".equals(node.getNodeName())) { 
					String enc_key_anhui = node.getTextContent();
					info.setEnc_key_anhui(enc_key_anhui);
				}else if ("mch_id_fujian".equals(node.getNodeName())) { 
					String mch_id_fujian = node.getTextContent();
					info.setMch_id_fujian(mch_id_fujian);
				}else if ("enc_key_fujian".equals(node.getNodeName())) { 
					String enc_key_fujian = node.getTextContent();
					info.setEnc_key_fujian(enc_key_fujian);
				}
			}
		} catch (Exception e) {
			log.error("解析腾讯配置文件出现异常！", e);
		}

		return info;
	}

	/**
	 * 解析腾讯Q币充值的返回结果
	 */
	public static TencentGoodsInfo parseTencentResponse(String resultStr) {
		TencentGoodsInfo info = new TencentGoodsInfo();
		if (resultStr != null && !("".equals(resultStr))) {
			// 先用&进行解析
			String[] strArr = resultStr.split("&");
			for (int i = 0; i < strArr.length; i++) {
				String elementStr = strArr[i];
				String[] strArray = elementStr.split("=");
				String value = null;
				if (strArray.length >= 2) {
					value = strArray[1];
				}
				switch (i) {
				case 0:
					info.setRet(Integer.parseInt(value));
					break;
				case 1:
					info.setMch_id(value);
					break;
				case 2:
					info.setTran_date(value);
					break;
				case 3:
					info.setTran_seq(value);
					break;
				case 4:
					info.setIn_acct(value);
					break;
				case 5:
					info.setIn_acct_type(value);
					break;
				case 6:
					info.setSell_type(Integer.parseInt(value));
					break;
				case 7:
					info.setSell_sub_type(Integer.parseInt(value));
					break;
				case 8:
					info.setNum(value);
					break;
				case 9:
					info.setTime(value);
					break;
				case 10:
					info.setRet_msg(value);
					break;
				case 11:
					info.setSign(value);
					break;

				default:
					break;
				}

			}
		}

		return info;
	}

	/**
	 * 解析腾讯订单查询返回信息
	 * 
	 * @param result
	 * @return
	 */
	public static TencentGoodsInfo parseOderQueryResponse(String result) {
		TencentGoodsInfo info = new TencentGoodsInfo();
		if (StringUtils.isNotEmpty(result)) {
			// 先用&进行解析
			String[] strArr = result.split("&");
			if (strArr != null && strArr.length > 0) {
				for (int i = 0; i < strArr.length; i++) {
					String str = strArr[i];
					if (StringUtils.isNotEmpty(str)) {
						String[] keyValues = str.split("=");
						String value = null;
						if (keyValues.length >= 2) {
							value = keyValues[1];
						}
						switch (i) {
						case 0:
							info.setRet(Integer.parseInt(value));
							break;
						case 1:
							info.setMch_id(value);
							break;
						case 2:
							info.setTran_date(value);
							break;
						case 3:
							info.setTran_seq(value);
							break;
						case 4:
							info.setOrder_status(Integer.parseInt(value));
							break;
						case 5:
							info.setRet_msg(value);
							break;
						case 6:
							info.setSign(value);
							break;
						default:
							break;
						}
					}
				}
			}
		}
		return info;
	}

	// public static void main(String[] args) {
	// String str =
	// "ret=0&mch_id=qbas_cuixb9039zj&tran_date=20130514&tran_seq=2013051400000017&in_acct=672374349&in_acct_type=0&sell_type=1&sell_sub_type=0&num=1&time=1368513808&ret_msg=&sign=ce37718fc92485fac6806aba24659e44";
	// TencentGoodsInfo info = null;
	// info = parseTencentResponse(str);
	// }

}
